package com.hmusic.mymusic.controller;

import com.alibaba.fastjson.JSONObject;
import com.hmusic.mymusic.domain.Rankc;
import com.hmusic.mymusic.service.RankcService;
import com.hmusic.mymusic.utils.Consts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 歌单评分controller层
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/rank")
public class RankController {

    @Autowired
    private RankcService rankcService;

    /**
     * 歌单评分
     * @param rankc
     * @return
     */
    @PostMapping("/add")
    public Object addRank(@RequestBody Rankc rankc){
        JSONObject jsonObject = new JSONObject();
        try{
            rankcService.insert(rankc);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"评分成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,e.getMessage());
            return jsonObject;
        }
    }

    /**
     * 歌单平均分
     * @param song_list_id
     * @return
     */
    @GetMapping("/avgRank")
    @ResponseBody
    public Object avgRank(String song_list_id){
        return rankcService.rankOfSongListId(song_list_id);
    }

}
